package org.zxt.web.util;

public class MixBase {
    private static final int BASE_10 = 10;
    private static final int BASE_26 = 26;
    private static final int BASE_36 = 36;
    public static void convertToMixedBase(StringBuilder stringBuilder, long number) {
        if(number < BASE_10) {
            stringBuilder.append(number);
        } else if (number < BASE_36) {
            stringBuilder.append((char) ('A' + number - BASE_10));
        } else {
            long first = (number - BASE_36) / BASE_26;
            convertToMixedBase(stringBuilder, first+1);
            append26(stringBuilder, (number - BASE_36) % BASE_26);
        }
    }
    private static void append26(StringBuilder encoding, long firstDigit) {
        if(firstDigit < BASE_26){
            encoding.append((char) ('a' + firstDigit)); // 否则加入a-z
             return;
             }
        long remainingNumber = firstDigit/26;
        for(long i=0;i<remainingNumber;i++){
            encoding.append('z');
        }
        encoding.append((char) ('a' + firstDigit%26)); // 否则加入a-z
    }
    private static long pow(long n, int c) {
        if(c == 0) {
            return 1;
        }
        for(int i=0;i<c;i++){
            n*=n;
        }
        return n;
    }
    public static long convertToNumber(String abc){
        char[] cs = abc.toCharArray();
        long num = 0;
        int pos = cs.length;
        // Z 36
        // Zz 3626
        // Zzz 3626*26
        if(pos == 1) {
            char c = cs[0];
            if(c >= '0' && c <= '9'){
                num = c - '0';
            } else if(c >= 'A' && c <= 'Z') {
                num = c - 'A' + 10;
            }
        } else {
            long val = pow(26, pos - 2);
            int nnum = 0;
            for (int j = 1; j < pos-2; j++) {
                nnum += 35 * pow(26, j);
            }
            for(int i = 0, j = cs.length; i < j; i++){
                char c = cs[i];
                if(i != j - 1) {
                    if (c >= '0' && c <= '9') {
                        if(pos == 2) {
                            num = (c - '0' - 1) * 26 + 36;
                        } else {
                            num = (c - '0' - 1) * 26 * val + 35 * val + 36;
                            num += nnum;
                        }
                    } else if (c >= 'A' && c <= 'Z') {
                        if(pos == 2) {
                            num = (c - 'A' + 9) * 26 + 36;
                        } else {
                            num = (c - 'A' + 9) * 26 * val + 35 * val + 36;
                            num += nnum;
                        }
                    } else if (c >= 'a' && c <= 'z') {
                        num += (c - 'a') * pow(26, pos - i - 1);
                    }
                } else {
                    if (c >= 'a' && c <= 'z') {
                        num += (c - 'a');
                    } else {
                        throw new NumberFormatException("xxx");
                    }
                }
            }
        }
        return num;
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for(long i=0;i<1000000;i++){
            sb.delete(0, sb.length());
            convertToMixedBase(sb, i);
            String conv = sb.toString();
            long val = convertToNumber(conv);
            if(i != val){
                System.out.println(i);
            }
        }
    }
}
